package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.gulimall.member.exception.MemberException;
import com.atguigu.gulimall.member.feign.coupon.MemberCouponService;
import com.atguigu.gulimall.member.vo.MemberUserLoginVo;
import com.atguigu.gulimall.member.vo.UserRegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员
 *
 * @author ShiJieLin
 * @email ShiJieLin@gmail.com
 * @date 2022-03-29 10:50:45
 */
@RestController
@RequestMapping("member/member")
@RefreshScope
@Slf4j
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Resource
    private MemberCouponService memberCouponService;

    @Value("${member.user.name}")
    private String name;
    @Value("${member.user.age}")
    private Integer age;

    @PostMapping(value = "weixin/login")
    public R weixinLogin(@RequestParam("accessTokenInfo") String accessTokenInfo) {

        MemberEntity memberEntity = null;
        try {
            memberEntity = memberService.login(accessTokenInfo);

            if(null != memberEntity){
                return R.ok().setData(memberEntity);
            }else {
                return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),
                        BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
            }
        } catch (Exception e) {
            log.info("MemberController微信登陆出错", e);
            return null;
        }
    }

    @PostMapping(value = "/login")
    public R login(@RequestBody MemberUserLoginVo vo){

        MemberEntity memberEntity = memberService.login(vo);

        if(null == memberEntity){
           return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),
                   BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }
        return R.ok().setData(memberEntity);
    }


    /**
     * @param vo
     * @return
     */
    @PostMapping(value = "/register")
    public R register(@RequestBody UserRegisterVo vo){

        try {
            memberService.register(vo);
        } catch (MemberException e) {
            return R.error(e.getBizCodeEnum().getCode(), e.getBizCodeEnum().getMsg());
        }
        return R.ok();
    }

    public R testNacosConfig(){

        log.info("-------------> " + name + ": " + age);
        return R.ok().put("name", name).put("age", age);
    }

    @RequestMapping("/coupon/list")
    public R testFeign(){

        MemberEntity member = new MemberEntity();
        member.setNickname("Tom");
        R feign = memberCouponService.testFeign();

        return R.ok().put("member", member).put("coupon", feign.get("coupon"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
